<?php


namespace app\api\controller;

use app\common\model\SignIn;
use app\common\controller\ApiController;
use think\App;
class sign extends ApiController
{

    public function __construct()
    {
        parent::__construct();
        $this->model = new SignIn();
    }

    public function signIn(){
        $data['uid']= input('uid','');
        $data['sign_date']=date('Y-m-d H:i:s',input('sign_date',''));
        $data['type']=input('type','') == 1 ? '签到':'签退';
        if($data['type'] == '签到' ) {
            $info = $this->model->where(['uid'=>$data['uid']])->order('create_time DESC')->find();
            if($info){
                $info= $info->toArray();
            }
            if($info['type']=='签到'){
                self::_result(0,'请勿重复签到',[]);
            }
            $data['district'] = input('district', '');
            $data['town'] = input('town', '');
            $data['community'] = input('community', '');
            $data['party']= input('party','');
        }else{
            $info = $this->model->where(['type'=>'签到','uid'=>$data['uid']])->order('create_time DESC')->find()->toArray();
            $signin = $this->model->where(['type'=>'签到','uid'=>$info['uid']])->count();
            $signout = $this->model->where(['type'=>'签退','uid'=>$info['uid']])->count();
            if($signout >= $signin){
                self::_result(0,'请勿再次签退',[]);
            }
            $data['district'] = $info['district'];
            $data['town'] = $info['town'];
            $data['community'] = $info['community'];
            $data['party'] = $info['party'];
            $data['image']=ltrim(input('image',''),'www.sbd.com');
            $data['content']=input('content','');
        }
        $data['create_time']=date('Y-m-d H:i:s',time());
        $data['update_time']=date('Y-m-d H:i:s',time());
        $id = $this->model->insertGetId($data);
        if ($id) {
            self::_result(0,'签到/签退成功',[]);
        } else {
            self::_result(-1,'签到/签退失败',[]);
        }
    }

    public function uploadPic(){
        $file = request()->file('file');
        $temp = explode(".", $_FILES["file"]["name"]);
        $extension = end($temp);
        if(!in_array($extension, array("jpeg","jpg","png"))){
            self::_result(-1,'格式不正确',[]);
        }
        $savename = \think\facade\Filesystem::disk('public')->putFile('upload', $file);
        // windows系统中路径反斜杠处理
        $savename = str_replace('\\', '/', $savename);
        self::_result(0,'上传成功',['url'=>$_SERVER['HTTP_HOST'].'/'.$savename]);
    }
}